package org.jeecg.modules.com.pointoperation.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.com.pointoperation.entity.VerifyConsumeRecord;
import org.jeecg.modules.com.pointoperation.mapper.VerifyConsumeRecordMapper;
import org.jeecg.modules.com.pointoperation.service.IVerifyConsumeRecordService;
import org.jeecg.modules.com.pointoperation.utils.SignatureUtil;
import org.jeecg.modules.com.pointoperation.utils.StringClass;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: verify_consume_record
 * @Author: jeecg-boot
 * @Date:   2020-05-05
 * @Version: V1.0
 */
@Service
public class VerifyConsumeRecordServiceImpl extends ServiceImpl<VerifyConsumeRecordMapper, VerifyConsumeRecord> implements IVerifyConsumeRecordService {

    @Value("APP_URL_POINT")
    String APP_URL;


    @DS(value = "multi-datasource2")
    @Transactional
    public Boolean insertVerifyRecord(VerifyConsumeRecord verifyConsumeRecord) {

        int result = this.getBaseMapper().insert(verifyConsumeRecord);
        return result > 0 ? true : false;
    }


    @Override
    @DS(value = "multi-datasource3")
    @Transactional
    public Boolean insertVerifyRecord2(VerifyConsumeRecord verifyConsumeRecord) {

        int result = this.getBaseMapper().insert(verifyConsumeRecord);
        return result > 0 ? true : false;
    }

    public  static String PPPCHANNEL = "BJQ2";

    public String getPPPCHANNEL() {
        return PPPCHANNEL;
    }

    public void setPPPCHANNEL(String pppchannel) {
        PPPCHANNEL = pppchannel;
    }


    public JSONObject addPoint(String member,int boNo, int totalPoint){
        JSONObject ret = new JSONObject();
        try {
            //时间戳
            String ts = String.valueOf(System.currentTimeMillis() / 1000);
            //生成签名
            JSONObject json = new JSONObject();
            json.put("userNo",member);
            json.put("boNo",boNo);
            json.put("totalPoint",totalPoint);
            json.put("ts",ts);
            String md5Key = "xuanzhen";
            String sign = SignatureUtil.makeSignature(json,md5Key);

            JSONObject jsonObject = addPointHandle(member, boNo,totalPoint,ts,sign);

/*            ret.setStatus(Integer.parseInt(jsonObject.getString("status")));
            ret.setMessage(jsonObject.getString("message"));
            ret.setPoint(Integer.parseInt(jsonObject.getString("point")));
            ret.setSn(jsonObject.getString("sn"));
            Map<String,Object> map = new HashMap<>();
            map.put("BagNo",jsonObject.getString("BagNo"));
            ret.setMemberExt(map);*/
        } catch (Exception e) {
            log.error(e.toString()+ "加分接口异常");
//            ret.setStatus(2);
//            ret.setMessage("加分接口异常");
        }
        return ret;
    }


    /**
     * 增加积分
     * @param openId
     * @param boNo
     * @param point
     * @param ts
     * @param sign
     * @return
     */
    public  JSONObject addPointHandle(String openId, Integer boNo, Integer point, String ts, String sign) {

        /**
         * 获取积分平台的channel
         */
        String pppchannel = "BJQ2";
        if (StringUtils.isBlank(pppchannel)) {
            pppchannel = "default";
        }
        pppchannel = PPPCHANNEL;

        //生成签名
        JSONObject json = new JSONObject();
        json.put("userNo",openId);
        json.put("boNo",boNo);
        json.put("totalPoint",point);
        json.put("ts",ts);
        //md5key
        String md5Key = "xuanzhen";
        String mySign = SignatureUtil.makeSignature(json,md5Key);

        RestTemplate restTemplate = new RestTemplate();
        JSONObject result = restTemplate.getForObject(APP_URL + "addPointStd?userNo=" + openId + "&boNo=" + boNo
                + "&count=" + point + "&ts=" + ts + "&sign=" + mySign+"&channel="+pppchannel, JSONObject.class);
        return result;

    }
}
